#include "WidgetScene.h"
#include <QtOpengl/QGLWidget>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QGraphicsItem>
#include <QtGui/QPainter>
#include <QtGui/QPaintEngine>
#include <QtGui/QGRaphicsProxyWidget>
#include <QtGui/QSizePolicy>

WidgetScene::WidgetScene(void):view(0),proxyw(0)
{

}
void WidgetScene::setSceneRect(const QRectF &rect)
{
	if(view)
	view->resize(rect.width(),rect.height());
	if(proxyw)
		proxyw->setGeometry(rect);
	QGraphicsScene::setSceneRect(rect);
}

void WidgetScene::addView(Multiview *_view)
{
	view=_view;
	view->setGeometry(0,0,800,600);

	QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        sizePolicy.setHeightForWidth(view->sizePolicy().hasHeightForWidth());
        view->setSizePolicy(sizePolicy);
	QGraphicsProxyWidget *proxyw=QGraphicsScene::addWidget(view);
	
	foreach (QGraphicsItem *item, items()) 
	{
        item->setFlag(QGraphicsItem::ItemIsMovable);
        item->setCacheMode(QGraphicsItem::DeviceCoordinateCache);

		
  
		item->setPos(0,0);
    }

	
//proxyw->bac

	proxyw->setOpacity(0.5);
	proxyw->setGeometry(sceneRect());

	printf("sf");
}

void WidgetScene::drawBackground(QPainter *painter, const QRectF &)
{
    if (painter->paintEngine()->type() != QPaintEngine::OpenGL) 
	{
        qWarning("OpenGLScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view");
        return;
    }

    glClearColor(1, 0, 0,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
 //   gluPerspective(70, width() / height(), 0.01, 1000);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();


	if(view)
	view->fakePaintEvent();

    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

	glViewport(sceneRect().x(),sceneRect().y(),sceneRect().width(),sceneRect().height());


}

WidgetScene::~WidgetScene(void)
{
}


void WidgetScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsScene::mouseMoveEvent(event);
    if (event->isAccepted())
        return;
}

void WidgetScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsScene::mousePressEvent(event);
    if (event->isAccepted())
        return;
}

void WidgetScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsScene::mouseReleaseEvent(event);
    if (event->isAccepted())
        return;
}

void WidgetScene::wheelEvent(QGraphicsSceneWheelEvent *event)
{
    QGraphicsScene::wheelEvent(event);
    if (event->isAccepted())
        return;
}
